package com.zt.daoImpl;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import com.zt.comm.Const;
import com.zt.dao.ContractDao;
import com.zt.page.Page;
import com.zt.pojo.Contract;
import com.zt.utils.HibernateSessionFactory;

@Repository("contractDao")
public class ContractDaoImpl extends HibernateSessionFactory implements ContractDao{
	@Override
	public int getTotalNum() {
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String sql = "select count(*) from Contract";
		int totalNum  = Integer.parseInt(session.createQuery(sql).uniqueResult().toString());
		trans.commit();
		session.close();
		return totalNum;
	}
	@Override
	public Page<Contract> find(Page<Contract> page) {
		page.setTotalNum(getTotalNum());
		int pageNum=page.getPageNo();
		System.out.println("第一页页码："+pageNum);
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String hql = "FROM Contract";	
		List<Contract> list = session.createQuery(hql).setMaxResults(3).setFirstResult((page.getPageNo()-1)*3).list();
		page.setList(list);
		System.out.println("页码："+page.getPageNo());
		System.out.println("条数："+page.getPageSize());
		System.out.println("集合："+page.getList());
		trans.commit();
		session.close();
		return page;
	}
	
	@Override
	public Contract findByContract_id(Contract con) {
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String hql = "FROM Contract where contract_id=:contract_id";	
		Contract resultCon = (Contract) session.createQuery(hql).setParameter("contract_id", con.getContract_id()).uniqueResult();
		System.out.println("结果："+resultCon);
		trans.commit();
		session.close();
		return resultCon;
	}
	
	@Override
	public Contract restYearsByEmpno(Contract con) {
		String hql = "From t_contract where empno=? ";
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		Contract resultCon = (Contract) session.createQuery(hql).setParameter(0, con.getEmpno()).uniqueResult();
		trans.commit();
		session.close();
		return resultCon;
	}

	@Override
	public String addContract(Contract con) {
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String msg=Const.ERROR;
		try {
			session.save(con);
			msg=Const.SUCCESS;
			trans.commit();
			session.close();
		} catch (Exception e) {	
			trans.rollback();
			session.close();
			e.printStackTrace();
		}
		return msg;
	}

	@Override
	public String updateContract(Contract con) {
		Session session = getSession();
		System.err.println("实现类中更新方法的con="+con);
		Transaction trans = session.beginTransaction();
		String msg=Const.ERROR;
		try {
			session.update(con);
			msg=Const.SUCCESS;
			trans.commit();
			session.close();
		} catch (Exception e) {	
			trans.rollback();
			session.close();
			e.printStackTrace();
		}
		return msg;
	}

	@Override
	public String changSignState(Contract con) {
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String msg=Const.ERROR;
		try {
			session.saveOrUpdate(con);
			msg=Const.SUCCESS;
			trans.commit();
			session.close();
		} catch (Exception e) {	
			trans.rollback();
			session.close();
			e.printStackTrace();
		}
		return msg;
	}
	@Override
	public String deleteContract(Contract con) {
		System.out.println("删除方法进来了");
		Session session = getSession();
		Transaction trans = session.beginTransaction();
		String msg=Const.ERROR;
		try {
			session.delete(con);
			msg=Const.SUCCESS;
			trans.commit();
			session.close();
		} catch (Exception e) {	
			trans.rollback();
			session.close();
			e.printStackTrace();
		}
		return msg;	
	}

}
